/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import model.pojo.User;
import model.pojo.Feedback;
import model.pojo.util.helper;

/**
 *
 * @author Yasith
 */
public class fbFeedback {

    public static boolean addFeedback(User u, String feedback) {

        EntityManager em = helper.getEMF().createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();

        Feedback f = new Feedback();
        f.setFeedback(feedback);
        f.setTimestamp(new Date());
        f.setUserID(u);

        em.persist(f);
        tx.commit();
        em.close();

        return true;
    }

    public static List<Feedback> getFeedback() {

        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createQuery("Select fb FROM Feedback fb ORDER BY fb.timestamp");
        List<Feedback> fb;
        try {
            fb = (List<Feedback>) query.getResultList();
        } catch (NoResultException ex) {
            fb = null;
        }
        newTx.commit();
        newEm.close();

        return fb;
    }
}
